home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / gencodec / source / writecatalogfiles.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  9KB  |  332 lines

  1. #include "WriteCatalogFiles.h"
  2. #include "Tools.h"
  3.  
  4. #include <stdio.h>
  5. #include <exec/types.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. /****************************************************************************************************************/
  12. /*****                                                                                                        *****/
  13. /**                                            WriteCatalogConstants                                                **/
  14. /*****                                                                                                        *****/
  15. /****************************************************************************************************************/
  16.  
  17. static BOOL WriteCatalogConstants(FILE *file,char *CatalogName)
  18. {
  19.     char    *file2;
  20.     char    *index;
  21.     char    *index2;
  22.     char    *variable;
  23.  
  24.     if (file2 = LoadFileInRAM(CatalogName, FALSE))
  25.     {
  26.         index = file2;
  27.  
  28.         while(index)
  29.         {
  30.             /* Search a descrition line */
  31.             /* Jump commands, comments lines */
  32.             while(index &&
  33.                   (*index=='#' || *index==';'))
  34.             {
  35.                 index = strchr(index,'\n');
  36.                 if (index)
  37.                     index++;
  38.             }
  39.  
  40.             /* Get description variable */ 
  41.             if (index)
  42.             {
  43.                 index2 = index;
  44.                 while(*index2 && *index2!=' ' && *index2!='\t' && *index2!='(') 
  45.                     index2++;
  46.                 if (*index2)
  47.                 {
  48.                     if (!(variable = AllocMemory(index2-index+1,FALSE)))
  49.                     {
  50.                         fcloseFile(file);
  51.                         FreeMemory(file2);
  52.                         return FALSE;
  53.                     }
  54.                     else
  55.                     {
  56.                         strncpy(variable,index,index2-index);
  57.                         variable[index2-index]='\0';
  58.                         fprintf(file,"extern const APTR _%s;\n",variable);
  59.                         fprintf(file,"#define %s ((APTR) &_%s)\n",variable,variable);
  60.                         FreeMemory(variable);
  61.                     }
  62.                     index2 = strchr(index2,'\n');
  63.  
  64.                     /* Jump description string */
  65.                     do
  66.                     {
  67.                         index2++;
  68.                         index2 = strchr(index2,'\n');
  69.                     }while(index2 && *(index2-1)=='\\');
  70.                     if (index2)
  71.                         index = index2+1;
  72.                     else
  73.                         index = NULL;
  74.                 }
  75.                 else
  76.                     index = NULL;
  77.             }
  78.         }
  79.         FreeMemory(file2);
  80.         return TRUE;
  81.     }
  82.     else
  83.     {
  84.         DisplayMsg("Can't open catalog description file !!! \nPlease generate it with MUIBuilder !!! \n");
  85.         return FALSE;
  86.     }
  87. }
  88.  
  89. /****************************************************************************************************************/
  90. /*****                                                                                                        *****/
  91. /**                                     WriteCatalogStringsInitialisation                                           **/
  92. /*****                                                                                                        *****/
  93. /****************************************************************************************************************/
  94.  
  95. static BOOL WriteCatalogStringsInitialisation(FILE *file,char *CatalogName)
  96. {
  97.     char    *file2;
  98.     char    *index;
  99.     char    *index2;
  100.     char    *variable;
  101.     ULONG    cpt = 0;
  102.  
  103.     if (file2 = LoadFileInRAM(CatalogName,FALSE))
  104.     {
  105.         index = file2;
  106.         
  107.         while(index)
  108.         {
  109.             /* Search a descrition line */
  110.             /* Jump commands, comments lines */
  111.             while(index &&
  112.                   (*index=='#' || *index==';'))
  113.             {
  114.                 index = strchr(index,'\n');
  115.                 if (index)
  116.                     index++;
  117.             }
  118.  
  119.             /* Get description variable */ 
  120.             if (index)
  121.             {
  122.                 index2 = index;
  123.                 while(*index2 && *index2!=' ' && *index2!='\t' && *index2!='(') 
  124.                     index2++;
  125.                 if (*index2)
  126.                 {
  127.                     if (!(variable = AllocMemory(index2-index+1,FALSE)))
  128.                     {
  129.                         fcloseFile(file);
  130.                         FreeMemory(file2);
  131.                         return FALSE;
  132.                     }
  133.                     else
  134.                     {
  135.                         strncpy(variable,index,index2-index);
  136.                         variable[index2-index]='\0';
  137.                         fprintf(file,"const struct FC_Type _%s = { %d, \"",variable,cpt++);
  138.                         FreeMemory(variable);
  139.  
  140.                         /* Get description string */
  141.                         index2 = strchr(index2,'\n');
  142.                         index = index2 + 1;
  143.                         do
  144.                         {
  145.                             index2++;
  146.                             index2 = strchr(index2,'\n');
  147.                         }while(index2 && *(index2-1)=='\\');
  148.  
  149.                         if (index2==0)                    /* no "\n" in the last line of the file */
  150.                             index2=file2+strlen(file2);    /* index2=end_of_file */
  151.  
  152.                         if (!(variable = AllocMemory(index2-index+1,FALSE)))
  153.                         {
  154.                             fcloseFile(file);
  155.                             FreeMemory(file2);
  156.                             return FALSE;
  157.                         }
  158.                         else
  159.                         {
  160.                             strncpy(variable,index,index2-index);
  161.                             variable[index2-index]='\0';
  162.                             fprintf(file,"%s\" };\n",variable);
  163.                             FreeMemory(variable);
  164.                             if (index2)
  165.                                 index = index2 + 1;
  166.                             else
  167.                             index = NULL;
  168.                         }                
  169.                     }
  170.                 }
  171.                 else
  172.                     index = NULL;
  173.             }
  174.         }
  175.         FreeMemory(file2);
  176.         return TRUE;
  177.     }
  178.     else
  179.     {
  180.         DisplayMsg("Can't open catalog description file !!! \nPlease generate it with MUIBuilder !!! \n");
  181.         return FALSE;
  182.     }
  183. }
  184.  
  185. /****************************************************************************************************************/
  186. /*****                                                                                                        *****/
  187. /**                                         Write_Catalog_h_File                                                   **/
  188. /*****                                                                                                        *****/
  189. /****************************************************************************************************************/
  190.  
  191. BOOL Write_Catalog_h_File(char *Catalog_h_File,char *CatalogName,char *GetString)
  192. {
  193.     char    *name;
  194.     FILE    *file;
  195.  
  196.     if (file = fopenFile(Catalog_h_File,"w",FALSE))
  197.     {
  198.         if (!(name=AllocMemory(strlen(FilePart(CatalogName))+1,FALSE)))
  199.         {
  200.             fcloseFile(file);
  201.             return FALSE;
  202.         }
  203.         strcpy(name,FilePart(CatalogName));
  204.         remove_extend(name);
  205.  
  206.         fprintf(file,"#ifndef %s_CAT_H\n",name);
  207.         fprintf(file,"#define %s_CAT_H\n\n",name);
  208.         fprintf(file,"#include <exec/types.h>\n");
  209.         fprintf(file,"#include <libraries/locale.h>\n\n");
  210.         fprintf(file,"/* Prototypes */\n");
  211.         fprintf(file,"extern void OpenAppCatalog(struct Locale *, STRPTR);\n");
  212.         fprintf(file,"extern void CloseAppCatalog(void);\n");
  213.         fprintf(file,"extern char *%s(APTR);\n\n",GetString);
  214.  
  215.         fprintf(file,"/* Definitions */\n");
  216.         if (!WriteCatalogConstants(file,CatalogName))
  217.         {
  218.             FreeMemory(name);
  219.             fcloseFile(file);
  220.             return FALSE;
  221.         }
  222.  
  223.         fprintf(file,"\n#endif /* !%s_CAT_H */\n",name);
  224.  
  225.         FreeMemory(name);
  226.         fcloseFile(file);
  227.         return TRUE;
  228.     }
  229.     else
  230.     {
  231.         DisplayMsg("Unable to create Catalog_h File !!! \n");
  232.         return FALSE;
  233.     }
  234. }
  235.  
  236. /****************************************************************************************************************/
  237. /*****                                                                                                        *****/
  238. /**                                             Write_Catalog_c_File                                               **/
  239. /*****                                                                                                        *****/
  240. /****************************************************************************************************************/
  241.  
  242. BOOL Write_Catalog_c_File(char *Catalog_c_File,char *CatalogName,char *GetString)
  243. {
  244.     char    *name;
  245.     FILE    *file;
  246.  
  247.     if (file = fopenFile(Catalog_c_File,"w",FALSE))
  248.     {
  249.         if (!(name=AllocMemory(strlen(FilePart(CatalogName))+1,FALSE)))
  250.         {
  251.             fcloseFile(file);
  252.             return FALSE;
  253.         }
  254.         strcpy(name,FilePart(CatalogName));
  255.         remove_extend(name);
  256.  
  257.         fprintf(file,"/* Prototypes */\n");
  258.         fprintf(file,"#ifdef __GNUC__\n");
  259.         fprintf(file,"#include <proto/locale.h>\n");
  260.         fprintf(file,"#include <proto/dos.h>\n");
  261.         fprintf(file,"#else\n");
  262.         fprintf(file,"#include <proto/locale.h>\n");
  263.         fprintf(file,"#include <clib/dos_protos.h>\n");
  264.         fprintf(file,"\nextern struct Library *LocaleBase;\n\n");
  265.         fprintf(file,"#endif /* __GNUC__ */\n\n");
  266.         fprintf(file,"\n\n");
  267.         fprintf(file,"static LONG %s_Version = 0;\n",name);
  268.         fprintf(file,"static const STRPTR %s_BuiltInLanguage = (STRPTR) \"english\";\n\n",name);
  269.         fprintf(file,"struct FC_Type\n");
  270.         fprintf(file,"{   LONG   ID;\n");
  271.         fprintf(file,"    char *Str;\n");
  272.         fprintf(file,"};\n\n");
  273.  
  274.         fprintf(file,"/* Definitions */\n");
  275.         if (!WriteCatalogStringsInitialisation(file,CatalogName))
  276.         {
  277.             FreeMemory(name);
  278.             fcloseFile(file);
  279.             return FALSE;
  280.         }
  281.  
  282.         fprintf(file,"\nextern void CloseAppCatalog(void);\n\n");
  283.         fprintf(file,"static struct Catalog *%s_Catalog = NULL;\n\n",name);
  284.         fprintf(file,"void OpenAppCatalog(struct Locale *loc, STRPTR language)\n");
  285.         fprintf(file,"{\n");
  286.         fprintf(file,"\tLONG tag, tagarg;\n\n");
  287.         fprintf(file,"\tCloseAppCatalog(); /* Not needed if the programmer pairs OpenAppCatalog\n");
  288.         fprintf(file,"\t\t\tand CloseAppCatalog right, but does no harm.  */\n\n");
  289.         fprintf(file,"\tif (%s_Catalog == NULL)\n",name);
  290.         fprintf(file,"\t{\n");
  291.         fprintf(file,"\t\tif (language == NULL)\n");
  292.         fprintf(file,"\t\t\ttag = TAG_IGNORE;\n");
  293.         fprintf(file,"\t\telse\n");
  294.         fprintf(file,"\t\t{\n");
  295.         fprintf(file,"\t\t\ttag = OC_Language;\n");
  296.         fprintf(file,"\t\t\ttagarg = (LONG) language;\n");
  297.         fprintf(file,"\t\t}\n");
  298.         remove_extend(CatalogName);
  299.         fprintf(file,"\t\t%s_Catalog = OpenCatalog(loc, (STRPTR) \"%s.catalog\",\n",name,FilePart(CatalogName));
  300.         add_extend(CatalogName,".cd");
  301.         fprintf(file,"\t\t\tOC_BuiltInLanguage,(LONG)%s_BuiltInLanguage,\n",name);
  302.         fprintf(file,"\t\t\ttag, tagarg,\n");
  303.         fprintf(file,"\t\t\tOC_Version, %s_Version,\n",name);
  304.         fprintf(file,"\t\t\tTAG_DONE);\n");
  305.         fprintf(file,"\t}\n");
  306.         fprintf(file,"}\n\n");
  307.         fprintf(file,"void CloseAppCatalog(void)\n");
  308.         fprintf(file,"{\n");
  309.         fprintf(file,"\tCloseCatalog(%s_Catalog);\n",name);
  310.         fprintf(file,"\t%s_Catalog = NULL;\n",name);
  311.         fprintf(file,"}\n\n");
  312.         fprintf(file,"char * %s(APTR fcstr)\n",GetString);
  313.         fprintf(file,"{\n");
  314.         fprintf(file,"\tchar *defaultstr;\n");
  315.         fprintf(file,"\tLONG strnum;\n\n");
  316.         fprintf(file,"\tstrnum = ((struct FC_Type *) fcstr)->ID;\n");
  317.         fprintf(file,"\tdefaultstr = ((struct FC_Type *) fcstr)->Str;\n\n");
  318.         fprintf(file,"\treturn(%s_Catalog ? (char *)GetCatalogStr(%s_Catalog, strnum, defaultstr) :\n",name,name);
  319.         fprintf(file,"\t\t\tdefaultstr);\n");
  320.         fprintf(file,"}\n");
  321.  
  322.         FreeMemory(name);
  323.         fcloseFile(file);
  324.         return TRUE;
  325.     }
  326.     else
  327.     {
  328.         DisplayMsg("Unable to create Catalog_c File !!! \n");
  329.         return FALSE;
  330.     }
  331. }
  332.